<?php
/*
Plugin Name: Refined Media
Plugin URI: https://code.google.com/p/refined-media
Description: Refined Media is a plug-in for WordPress that re-writes the WordPress image shortcodes and adds jQuery slideshows.
Author: Jeremy Zilar
Version: 1.0
Author URI: http://silencematters.com
*/

function aphelia_img_shortcode($attr, $content = null) {
  $output = apply_filters('img_caption_shortcode', '', $attr, $content);
	if ( $output != '' )
		return $output;
 
	extract(shortcode_atts(array(
		'id'	=> '',
		'align'	=> '',
		'width'	=> '',
		'title'	=> '',
		'caption' => ''
	), $attr));
 
	if ( 1 > (int) $width || empty($caption) )
		return $content;
 
	if ( $id ) $id = 'id="' . $id . '" ';
 
	return '<div ' . $id . 'class="w' . (0 + (int) $width) . ' ' . $align . '">'
	. do_shortcode( $content ) . '<span class="credit"></span> <span class="caption">' . $caption . $title . '</span></div>';
}
 
add_shortcode('wp_caption', 'aphelia_img_shortcode');
add_shortcode('caption', 'aphelia_img_shortcode');




// Fixing the WP Galery Function
// * This implements the functionality of the Gallery Shortcode for displaying
// * WordPress images on a post.

//deactivate WordPress function
remove_shortcode('gallery', 'gallery_shortcode');
 
//activate own function
add_shortcode('gallery', 'aphelia_gallery_shortcode');
 
//the own renamed function
function aphelia_gallery_shortcode($attr) {
	global $post, $wp_locale;

	static $instance = 0;
	$instance++;

	// Allow plugins/themes to override the default gallery template.
	$output = apply_filters('post_gallery', '', $attr);
	if ( $output != '' )
		return $output;

	// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
	if ( isset( $attr['orderby'] ) ) {
		$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
		if ( !$attr['orderby'] )
			unset( $attr['orderby'] );
	}

	extract(shortcode_atts(array(
		'order'      => 'ASC',
		'orderby'    => 'menu_order ID',
		'id'         => $post->ID,
		'imagetag'    => 'div',
		'linktag'    => 'a',
		'imagetitle'    => $post->title,
		'captiontag' => 'div',
		'columns'    => 3,
		'size'       => 'large',
		'include'    => '',
		'exclude'    => ''
	), $attr));

	$id = intval($id);
	if ( 'RAND' == $order )
		$orderby = 'none';

	if ( !empty($include) ) {
		$include = preg_replace( '/[^0-9,]+/', '', $include );
		$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );

		$attachments = array();
		foreach ( $_attachments as $key => $val ) {
			$attachments[$val->ID] = $_attachments[$key];
		}
	} elseif ( !empty($exclude) ) {
		$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
		$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
	} else {
		$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
	}

	if ( empty($attachments) )
		return '';

	if ( is_feed() ) {
		$output = "\n";
		foreach ( $attachments as $att_id => $attachment )
			$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
		return $output;
	}

	$itemtag = tag_escape($itemtag);
	$captiontag = tag_escape($captiontag);
	$columns = intval($columns);
	$float = $wp_locale->text_direction == 'rtl' ? 'right' : 'left';

	$selector = "gallery-{$instance}";

	$output = apply_filters('gallery_style', "
		<div class='slideshow'>");
  
	$i = 0;
	foreach ( $attachments as $id => $attachment ) {
		$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_image($id, $size, false, false) : wp_get_attachment_image($id, $size, true, false);
    
		$output .= "
			<{$imagetag} class='w500'>
			  <{$linktag} id='{$attachment->post_title}'>$link</{$linktag}>
			</{$imagetag}>";
	}

	$output .= "</div>\n";
	
	$output .= "<div class='arrow-nav'><a id='prev1' href='#'><span><</span></a> <a id='next1' href='#'><span>></span></a></div>\n";
	
	if ( $captiontag && trim($attachment->post_excerpt) ) {
		$output .= "
			<{$captiontag} class='caption'>
			" . wptexturize($attachment->post_excerpt) . "
			</{$captiontag}>";
	}
	
	

	return $output;
} ?>